package com.cloudkinto.controller.order.outbound;


import com.cloudkinto.base.BaseController;
import com.cloudkinto.bo.order.outbound.*;
import com.cloudkinto.common.annotations.UserSessionRequired;
import com.cloudkinto.common.web.PageResult;
import com.cloudkinto.common.web.SingleResult;
import com.cloudkinto.enumType.order.outbound.OutboundOriginEnum;
import com.cloudkinto.service.order.outbound.OutboundImportExportService;
import com.cloudkinto.service.order.outbound.OutboundOldExportService;
import com.cloudkinto.service.order.outbound.OutboundService;
import com.cloudkinto.vo.common.CommonBatchOperationVo;
import com.cloudkinto.vo.order.outbound.NormalOutboundDetail;
import com.cloudkinto.vo.order.outbound.NormalOutboundVo;
import com.cloudkinto.vo.order.outbound.TransferOutboundDetail;
import com.cloudkinto.vo.order.outbound.TransferOutboundVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 出库单 前端控制器
 * </p>
 *
 * @author long
 * @since 2024-11-21
 */
@RestController
@RequiredArgsConstructor
@Api(tags = "Oms出库单模块")
@RequestMapping("/api/admin/oms/order/outbound")
public class OrderOutboundOmsController extends BaseController {
    private final OutboundService outboundService;

    private final OutboundImportExportService outboundImportExportService;

    private final OutboundOldExportService outboundOldExportService;


    @UserSessionRequired
    @PutMapping("getJdyTrackNumberManual")
    @ApiOperation(value = "手动获取jdy单号", notes = "手动获取jdy单号")
    @ApiResponses(@ApiResponse(code = 200, message = "操作成功"))
    public SingleResult dealError(@RequestBody @Validated List<String> platformOrderNos) {
        outboundService.getJdyTrackNumberManual(platformOrderNos);
        return SingleResult.success();
    }

    @UserSessionRequired
    @PostMapping("/normalSave")
    @ApiOperation(value = "一件代发出库单提交/草稿", notes = "一件代发出库单提交/草稿")
    public SingleResult normalSubmit(@RequestBody @Validated NormalOrderOutboundSaveBo bo) {
        outboundService.normalSave(bo, getCurrentUserId(), getUserName(), getCompanyId(), getTenantId(), OutboundOriginEnum.one);
        return SingleResult.success("新增成功");
    }

    @UserSessionRequired
    @PostMapping("/transferSave")
    @ApiOperation(value = "中转出库单提交/草稿", notes = "中转出库单提交/草稿")
    public SingleResult transferSave(@RequestBody @Validated TransferOrderOutboundSaveBo bo) {
        outboundService.transferSave(bo, getCurrentUserId(), getUserName(), getTenantId(), getCompanyId());
        return SingleResult.success("新增成功");
    }

    @UserSessionRequired
    @DeleteMapping("/{id}")
    @ApiOperation(value = "出库单根据id删除", notes = "出库单根据id删除")
    public SingleResult delete(@PathVariable("id") Long id) {
        outboundService.delete(id, getCurrentUserId());
        return SingleResult.success("删除成功");
    }

    @UserSessionRequired
    @GetMapping("/pageInit")
    @ApiOperation(value = "出库单查询初始化", notes = "出库单查询初始化")
    public SingleResult<Map<String, Object>> pageInit() {
        return outboundService.pageInit(getCurrentUserId(), getTenantId(), getCompanyId(), "oms");
    }

    @UserSessionRequired
    @PostMapping("/getTransferListPage")
    @ApiOperation(value = "批发出库单查询", notes = "批发出库单查询")
    @ApiResponses(@ApiResponse(code = 200, message = "操作成功", response = TransferOutboundVo.class))
    public PageResult<TransferOutboundVo> getTransferListPage(@RequestBody @Validated TransferOrderOutboundQueryBo bo) {
        bo.setTenantId(getTenantId());
        bo.setUserId(getCurrentUserId());
        bo.setSource("oms");
        bo.setCompanyId(getCompanyId());
        return outboundService.getTransferListPage(bo);
    }

    @UserSessionRequired
    @PutMapping("/submitDraft")
    @ApiOperation(value = "出库单提交草稿", notes = "出库单提交草稿")
    @ApiResponses(@ApiResponse(code = 200, message = "操作成功", response = CommonBatchOperationVo.class))
    public SingleResult submitDraft(@RequestBody @Validated OutboundSubmitDraftBo bo) {
        return SingleResult.success(outboundService.submitDraft(bo, getTenantId(), getCurrentUserId(), getUserName()));
    }

    @UserSessionRequired
    @PutMapping("turnIntoDraft")
    @ApiOperation(value = "出库单转为草稿")
    @ApiResponses(@ApiResponse(code = 200, message = "操作成功", response = CommonBatchOperationVo.class))
    public SingleResult turnIntoDraft(@RequestBody @Validated OutboundTurnIntoDraftBo bo) {
        return SingleResult.success(outboundService.turnIntoDraft(bo, getCurrentUserId(), getUserName()));
    }

    @UserSessionRequired
    @PutMapping("/cancel")
    @ApiOperation(value = "出库单取消", notes = "出库单取消")
    @ApiResponses(@ApiResponse(code = 200, message = "操作成功", response = CommonBatchOperationVo.class))
    public SingleResult cancel(@RequestBody @Validated OutboundCancelBo bo) {
        return SingleResult.success(outboundService.cancel(bo, getCurrentUserId(), getUserName(), getCompanyId()));
    }

    @UserSessionRequired
    @PutMapping("/batchUpdateWhAndChannel")
    @ApiOperation(value = "批量修改仓库和渠道", notes = "批量修改仓库和渠道")
    public SingleResult<CommonBatchOperationVo> batchUpdateWhAndChannel(@RequestBody @Validated OutboundBatchUpdateWhAndChannelBo bo) {
        return SingleResult.success(outboundService.batchUpdateWhAndChannel(bo, getCurrentUserId(), getUserName(), getCompanyId()));
    }

    @UserSessionRequired
    @PostMapping("/getTransferStatusSta")
    @ApiOperation(value = "批发单状态统计", notes = "批发单状态统计")
    public SingleResult getTransferStatusSta(@RequestBody @Validated TransferOrderOutboundQueryBo bo) {
        bo.setTenantId(getTenantId());
        bo.setUserId(getCurrentUserId());
        bo.setSource("oms");
        bo.setCompanyId(getCompanyId());
        return outboundService.getTransferStatusSta(bo);
    }

    @UserSessionRequired
    @PostMapping("/getNormalListPage")
    @ApiOperation(value = "一件代发出库单查询（分页）", notes = "一件代发出库单查询（分页）")
    @ApiResponses(@ApiResponse(code = 200, message = "", response = NormalOutboundVo.class))
    public PageResult<NormalOutboundVo> getNormalListPage(@RequestBody @Validated NormalOrderOutboundQueryBo bo) {
        bo.setTenantId(getTenantId());
        bo.setUserId(getCurrentUserId());
        bo.setSource("oms");
        bo.setCompanyId(getCompanyId());
        return outboundService.getNormalListPage(bo);
    }

    @UserSessionRequired
    @PostMapping("/getNormalStatusSta")
    @ApiOperation(value = "一件代发状态统计", notes = "一件代发状态统计")
    public SingleResult getNormalStatusSta(@RequestBody @Validated NormalOrderOutboundQueryBo bo) {
        bo.setTenantId(getTenantId());
        bo.setUserId(getCurrentUserId());
        bo.setSource("oms");
        bo.setCompanyId(getCompanyId());
        return outboundService.getNormalStatusSta(bo);
    }

    @UserSessionRequired
    @GetMapping("/normalOutboundDetail/{id}")
    @ApiOperation(value = "一件代发查看详情", notes = "一件代发查看详情")
    @ApiResponses(@ApiResponse(code = 200, message = "", response = NormalOutboundDetail.class))
    public SingleResult normalOutboundDetail(@PathVariable("id") Long id) {
        return SingleResult.success("查询成功", outboundService.normalOutboundDetail(id, getCurrentUserId(), "oms"));
    }

    @UserSessionRequired
    @GetMapping("/transferOutboundDetail/{id}")
    @ApiOperation(value = "批发单查看详情", notes = "批发单查看详情")
    @ApiResponses(@ApiResponse(code = 200, message = "", response = TransferOutboundDetail.class))
    public SingleResult transferOutboundDetail(@PathVariable("id") Long id) {
        return SingleResult.success("查询成功", outboundService.transferOutboundDetail(id, getCurrentUserId(), "oms"));
    }

    @UserSessionRequired
    @GetMapping("/getWholesaleOutboundImportTemplate1")
    @ApiOperation(value = "获取批发出库导入模板1", notes = "获取批发单导入模板1")
    public void getWholesaleOutboundImportTemplate1(HttpServletResponse response) throws IOException {
        outboundImportExportService.getWholesaleOutboundImportTemplate1(response, getTenantId(), getCompanyId());
    }

    @UserSessionRequired
    @GetMapping("/getWholesaleOutboundImportTemplate2")
    @ApiOperation(value = "获取批发出库导入模板2", notes = "获取批发单导入模板2")
    public void getWholesaleOutboundImportTemplate2(HttpServletResponse response) throws IOException {
        outboundImportExportService.getWholesaleOutboundImportTemplate2(response, getTenantId(), getCompanyId());
    }

    @UserSessionRequired
    @PostMapping("/importTransferOutbound1")
    @ApiOperation(value = "批发单导入1")
    public SingleResult importTransferOutbound1(MultipartFile file) {
        return SingleResult.success(outboundImportExportService.importTransferOutbound1(file, getTenantId(), getCurrentUserId(), getUserName(), getCompanyId()));
    }

    @UserSessionRequired
    @PostMapping("/importTransferOutbound2")
    @ApiOperation(value = "批发单导入2")
    public SingleResult importTransferOutbound2(MultipartFile file) {
        return SingleResult.success(outboundImportExportService.importTransferOutbound2(file, getTenantId(), getCurrentUserId(), getUserName(), getCompanyId()));
    }

    @UserSessionRequired
    @GetMapping("/getNormalOutboundImportTemplate1")
    @ApiOperation(value = "获取一件代发导入模板1", notes = "获取一件代发导入模板1")
    public void getNormalOutboundImportTemplate1(HttpServletResponse response) throws IOException {
        outboundImportExportService.getNormalOutboundImportTemplate1(response, getTenantId(), getCompanyId());
    }

    @UserSessionRequired
    @GetMapping("/getNormalOutboundImportTemplate2")
    @ApiOperation(value = "获取一件代发导入模板2", notes = "获取一件代发导入模板2")
    public void getNormalOutboundImportTemplate2(HttpServletResponse response) throws IOException {
        outboundImportExportService.getNormalOutboundImportTemplate2(response, getTenantId(), getCompanyId());
    }

    @UserSessionRequired
    @PostMapping("/importNormalOutbound1")
    @ApiOperation(value = "一件代发导入1", notes = "一件代发导入1")
    public SingleResult importNormalOutbound1(MultipartFile file) {
        return SingleResult.success(outboundImportExportService.importNormalOutbound1(file, getTenantId(), getCurrentUserId(), getUserName(), getCompanyId()));
    }

    @UserSessionRequired
    @PostMapping("/importNormalOutbound2")
    @ApiOperation(value = "一件代发导入2", notes = "一件代发导入2")
    public SingleResult importNormalOutbound2(MultipartFile file) {
        return SingleResult.success(outboundImportExportService.importNormalOutbound2(file, getTenantId(), getCurrentUserId(), getUserName(), getCompanyId()));
    }

    @UserSessionRequired
    @PutMapping("/importNormalOutboundTrackingNumber")
    @ApiOperation(value = "一件代发导入跟踪号", notes = "一件代发导入跟踪号")
    @ApiResponses(@ApiResponse(code = 200, message = "操作成功", response = CommonBatchOperationVo.class))
    public SingleResult importNormalOutboundTrackingNumber(MultipartFile file) {
        return SingleResult.success(outboundImportExportService.importNormalOutboundTrackingNumber(file, getTenantId(), getCurrentUserId(), getUserName(), getCompanyId()));
    }

    @UserSessionRequired
    @PutMapping("/importNormalOutboundExpress")
    @ApiOperation(value = "一件代发导入面单", notes = "一件代发导入面单")
    @ApiResponses(@ApiResponse(code = 200, message = "操作成功", response = CommonBatchOperationVo.class))
    public SingleResult importNormalOutboundExpress(@RequestBody @Validated ImportNormalOutboundExpressBo bo) {
        return SingleResult.success(outboundImportExportService.importNormalOutboundExpress(bo, getTenantId(), getCurrentUserId(), getUserName()));
    }

    @UserSessionRequired
    @PostMapping("/normalOutboundExport")
    @ApiOperation(value = "一件代发导出oms", notes = "一件代发导出oms")
    public void normalOutboundExport(@RequestBody @Validated NormalOrderOutboundQueryBo bo, HttpServletResponse response) throws IOException {
        bo.setTenantId(getTenantId());
        bo.setUserId(getCurrentUserId());
        bo.setSource("oms");
        bo.setCompanyIdList(Collections.singletonList(getCompanyId()));
        outboundImportExportService.normalOutboundExportOms(bo, getCompanyId(), response);
    }

    @UserSessionRequired
    @PostMapping("/normalOutboundExportBySimple")
    @ApiOperation(value = "一件代发导出简单")
    public void normalOutboundExportBySimple(@RequestBody @Validated NormalOrderOutboundQueryBo bo, HttpServletResponse response) throws IOException {
        bo.setTenantId(getTenantId());
        bo.setUserId(getCurrentUserId());
        bo.setSource("oms");
        bo.setCompanyIdList(Collections.singletonList(getCompanyId()));
        outboundImportExportService.normalOutboundExportBySimple(bo, response);
    }

    @UserSessionRequired
    @PostMapping("/transferOutboundExportBySimple")
    @ApiOperation(value = "批发单导出简单")
    public void transferOutboundExportBySimple(@RequestBody @Validated TransferOrderOutboundQueryBo bo, HttpServletResponse response) throws IOException {
        bo.setTenantId(getTenantId());
        bo.setUserId(getCurrentUserId());
        bo.setSource("oms");
        bo.setCompanyIdList(Collections.singletonList(getCompanyId()));
        outboundImportExportService.transferOutboundExportBySimple(bo,  response);
    }

    @UserSessionRequired
    @PostMapping("/transferOutboundExport")
    @ApiOperation(value = "批发单导出oms")
    public void transferOutboundExport(@RequestBody @Validated TransferOrderOutboundQueryBo bo, HttpServletResponse response) throws IOException {
        bo.setTenantId(getTenantId());
        bo.setUserId(getCurrentUserId());
        bo.setSource("oms");
        bo.setCompanyIdList(Collections.singletonList(getCompanyId()));
        outboundImportExportService.transferOutboundExportOms(bo,getCompanyId(), response);
    }

    @UserSessionRequired
    @PostMapping("/oldExport")
    @ApiOperation(value = "一期labella系统导出", notes = "一期labella系统导出")
    public void oldExport(@RequestBody @Validated NormalOrderOutboundQueryBo bo, HttpServletResponse response) throws IOException {
        bo.setTenantId(getTenantId());
        bo.setUserId(getCurrentUserId());
        bo.setSource("oms");
        bo.setCompanyIdList(Collections.singletonList(getCompanyId()));
        if (bo.getTemplateType() == 1) {
            outboundOldExportService.exportOrderFileUseZip(bo, response);
        } else if (bo.getTemplateType() == 2) {
            outboundOldExportService.exportOrderFileUseZipV2(bo, response);
        }
    }

    @UserSessionRequired
    @GetMapping("/getDeliveryOrder/{transferId}")
    @ApiOperation(value = "获取批发单的納品書", notes = "获取批发单的納品書")
    public void getDeliveryOrderZip(@PathVariable Long transferId, HttpServletResponse response) throws IOException {
        outboundImportExportService.getDeliveryOrderZip(transferId, response);
    }

    @UserSessionRequired
    @PostMapping("/getDeliveryOrderBatch")
    @ApiOperation(value = "获取批发单的納品書-批量")
    public void getDeliveryOrderBatch(Long[] ids, HttpServletResponse response) throws IOException {
        outboundImportExportService.getDeliveryOrderBatch(Arrays.asList(ids), response);
    }
}

